package cn.sunlelin.educationserver.service.Impl;

import cn.sunlelin.educationserver.entity.Discuss;
import cn.sunlelin.educationserver.mapper.DiscussMapper;
import cn.sunlelin.educationserver.service.DiscussService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class DiscussServiceImpl implements DiscussService {
    @Autowired
    private DiscussMapper discussMapper;

    @Override
    public Boolean createDiscuss(Discuss discuss) {
        discuss.setCreateTime(new Date());
        discussMapper.createDiscuss(discuss);
        return true;
    }

    @Override
    public List<Map<String, Object>> selectDiscussByCourseId(Integer courseId) {
        return discussMapper.selectDiscussByCourseId(courseId);
    }

    @Override
    public List<Map<String, Object>> selectDiscussByUserId(Integer userId) {
        return discussMapper.selectDiscussByUserId(userId);
    }

    @Override
    public Page<Map<String,Object>> getPageByUserId(Integer currentPage, Integer pageSize, Integer userId) {
        PageHelper.startPage(currentPage,pageSize);
        List<Map<String,Object>> courseList=discussMapper.selectDiscussByUserId(userId);
        return (Page<Map<String, Object>>) courseList;
    }

    @Override
    public Map<String, Object> selectDiscussById(Integer discussId) {
        return discussMapper.selectDiscussById(discussId);
    }

    @Override
    public Page<Map<String,Object>> getPageByCourseId(Integer currentPage, Integer pageSize, Integer courseId) {
        PageHelper.startPage(currentPage,pageSize);
        List<Map<String,Object>> courseList=discussMapper.selectDiscussByCourseId(courseId);
        return (Page<Map<String, Object>>) courseList;
    }

    @Override
    public Boolean addBrowseNum(Integer discussId) {
        if(discussMapper.selectDiscussById(discussId)==null)
            return false;
        discussMapper.addBrowseNum(discussId);
        return true;
    }
    @Override
    public Boolean addReplyNum(Integer discussId) {
        if(discussMapper.selectDiscussById(discussId)==null)
            return false;
        discussMapper.addReplyNum(discussId);
        return true;
    }

    @Override
    public Boolean minusReplyNum(Integer discussId) {
        if(discussMapper.selectDiscussById(discussId)==null)
            return false;
        discussMapper.minusReplyNum(discussId);
        return true;
    }

    @Override
    public Boolean deleteDiscuss(Integer discussId) {
        if(discussMapper.selectDiscussById(discussId)==null)
            return false;
        Discuss discuss=new Discuss();
        discuss.setDiscussId(discussId);
        discuss.setUpdateTime(new Date());
        discussMapper.deleteDiscuss(discuss);
        return true;
    }
}
